<?php

declare(strict_types=1);

namespace App\Controller;

use App\Model\Article;
use App\Services\EsService;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Elasticsearch\ClientBuilderFactory;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

/**
 * @AutoController()
 * Class SearchController
 * @package App\Controller
 */
class SearchController extends AbstractController
{
    /**
     * @Inject
     * @var EsService
     */
    protected $esService;

    protected $client;

    const DOC_TYPE = '_doc';
    const INDEX = 'articles';

    public function __construct(ClientBuilderFactory $builderFactory)
    {
        $builder = $builderFactory->create();
        $this->client = $builder->setHosts(['http://127.0.0.1:9200'])->build();
    }

    public function demo()
    {
        $articles = Article::query()->offset(1)->limit(1)->get();
        $this->esService->update($articles);
    }

    public function createDoc()
    {
        $id = uniqid();
        $response = $this->client->create([
            "id" => $id,
            "type" => self::DOC_TYPE,
            "index" => self::INDEX,
            "body" => [
                "title" => "title ${id}",
                "content" => "content ${id}",
                "created_at" => date('Y-m-d H:i:s')
            ]
        ]);
        var_dump($response);
    }


    public function updateDoc(RequestInterface $request)
    {
        $id = $request->input('id');
        $this->client->update([
            "id" => $id,
            "type" => self::DOC_TYPE,
            "index" => self::INDEX,
            "body" => [
                "doc" => [
                    "title" => "title ${id} 修改之后为" . date('Y-m-d H:i:s'),
                    "content" => "content ${id} 修改之后为" . date('Y-m-d H:i:s'),
                    "created_at" => date('Y-m-d H:i:s')
                ]
            ]
        ]);
    }

    public function deleteDoc(RequestInterface $request)
    {
        $id = $request->input('id');
        $this->client->delete([
            "id" => $id,
            "type" => self::DOC_TYPE,
            "index" => self::INDEX,
        ]);
    }

    public function search(RequestInterface $request)
    {
        $keyword = $request->input('keyword');
        return $this->client->search([
            "type" => self::DOC_TYPE,
            "index" => self::INDEX,
            "body" => [
                "query" => [
                    "bool" => [
                        "must" => [
                            [
                                "query_string" => [
                                    "query" => "*{$keyword}*"
                                ]
                            ]
                        ]
                    ],
                ],
                "sort" => [['created_at' => "desc"]],
                "from" => 0,
                "size" => 10,
                "highlight" => [
                    "pre_tags" => ["<b>"],
                    "post_tags" => ["</b>"],
                    "fields" => [
                        "title" => (object)[],
                        "content" => (object)[]
                    ]
                ]
            ]
        ]);
    }
}
